var prefix = jsctx + "/elecPrinter";

var elecPrinter = (function () {
    function init() {
        setTimeout(function () {//防止先于-读取缓存执行
            load();
        }, 10)
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });
    var h = $(window).height() - 80;

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get',
                    url: prefix + "/listPageElecPrinter",
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: false,
                    dataType: "json",
                    pagination: true,
                    singleSelect: false,
                    cache: false,
                    pageSize: 20,
                    pageList: [10, 20, 30],
                    pageNumber: 1,
                    sidePagination: "server",
                    paginationShowPageGo: true,
                    height: h,
                    queryParams: function (params) {
                        return {
                            limit: params.limit,
                            offset: (params.offset / params.limit) + 1,
                            printerNameSearch: $("#printerNameSearch").val().trim(),
                            loginStatusSearch: $("#loginStatusSearch").val().trim(),
                            statusSearch: $("#statusSearch").val().trim(),
                            printerRtuaSearch: $("#printerRtuaSearch").val().trim()
                        };
                    },
                    columns: [
                        {
                            title: '#', field: '', width: '50', align: 'left',
                            formatter: function (value, row, index) {
                                return index + 1;
                            }
                        },
                        {field: 'printerName', title: '打印机名称', align: 'left', width: '100'},
                        {field: 'printerRtua', title: '打印机逻辑地址', align: 'left', width: '100'},
                        {field: 'paperUsedCount', title: '已打印次数', align: 'left', width: '100'},
                        {field: 'paperRestCount', title: '剩余打印次数', align: 'left', width: '100'},
                        {field: 'warnCount', title: '纸张报警值', width: '100', align: 'left'},
                        // {field: 'accessUrl', title: '纸张状态', width: '100', align: 'left'},
                        {
                            field: 'loginStatus', title: '网络状态', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '在线'
                                } else if (value == '1') {
                                    return '离线'
                                }
                            }
                        },
                        {
                            field: 'status', title: '打印机状态', width: '100', align: 'left',
                            formatter: function (value, row, index) {
                                if (value == '0') {
                                    return '正常'
                                } else if (value == '1') {
                                    return '缺纸'
                                } else if (value == '2') {
                                    return '故障'
                                }
                            }
                        },
                        {
                            title: '操作',
                            field: '',
                            width: '200', align: 'center',
                            formatter: function (value, row, index) {
                                var a = '';
                                var b = '';
                                if (OperationsManagement_printer_update) {
                                    a = '<a class="btn btn-primary btn-sm ' + has_edit + '" href="#" title="修改" onclick="elecPrinter.editElecPrinter(\''
                                        + row.printerId + '\',' + '\'' + row.printerName + '\',' + '\'' + row.printerRtua + '\',' + '\'' + row.paperRestCount + '\',' + '\'' + row.warnCount
                                        + '\')">修改</a> ';
                                    b = '<a class="btn btn-danger btn-sm ' + has_edit + '" href="#" title="打印机站点" onclick="elecPrinter.elecPrinterStation(\''
                                        + row.printerId
                                        + '\')">打印机站点</a> ';
                                }
                                return a + b;
                            }
                        }
                    ]
                });
    }

    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }

    function goBack() {
        window.history.back();
    }

    function editElecPrinter(printerId, printerName, printerRtua, paperRestCount, warnCount) {
        $("#printerId").val(printerId)
        $("#printerName").val(printerName)
        $("#printerRtua").val(printerRtua)
        $("#paperRestCount").val(paperRestCount)
        $("#warnCount").val(warnCount)


        var addOrEdit = 'edit';
        showModel(addOrEdit)
    }

    function elecPrinterStation(printerId) {
        location.href = prefix + "/elecPrinterStation/" + printerId;
    }

    /*  模态框  */
    function showModel(addOrEdit) {
        if (addOrEdit == 'edit') {
            document.getElementById("printerRtua").readOnly = true;
        } else if ('add') {
            document.getElementById("printerRtua").readOnly = false;
            $("#printerId").val('')
            $("#printerName").val('')
            $("#printerRtua").val('')
            $("#paperRestCount").val('')
            $("#warnCount").val('')
        }

        $('#exampleModal').modal('show')

        $("#addElecPrinter").unbind("click").bind("click", function () {

            var formData = new FormData();
            formData.append("printerId", $("#printerId").val());
            formData.append("printerName", $("#printerName").val());
            formData.append("printerRtua", $("#printerRtua").val());
            formData.append("paperRestCount", $("#paperRestCount").val());
            formData.append("warnCount", $("#warnCount").val());
            $.ajax({
                url: prefix + "/saveOrUpdateElecPrinter",
                type: "post",
                data: formData,
                dataType: "json",
                processData: false, // 告诉jQuery不要去处理发送的数据，用于对data参数进行序列化处理 这里必须false
                contentType: false, // 告诉jQuery不要去设置Content-Type请求头
                success: function (data) {
                    if (data.status == 200) {
                        parent.layer.msg("操作成功");
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        $('#exampleModal').modal('hide')
                        reLoad();
                    } else {
                        parent.layer.alert(data.message);
                        $('#exampleModal').on('hidden.bs.modal', function () {
                            document.getElementById("contentForm").reset();
                        })
                        //  $('#exampleModal').modal('hide')
                    }

                }
            })
        })

    }

    function exportElecPrinter() {
        $.fileDownload(prefix + "/exportElecPrinter", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                printerNameSearch: $("#printerNameSearch").val().trim(),
                loginStatusSearch: $("#loginStatusSearch").val().trim(),
                printerRtuaSearch: $("#printerRtuaSearch").val().trim(),
                statusSearch: $("#statusSearch").val().trim(),
                count: $("#count").val().trim() == '' ? 0 : $("#count").val().trim(),
                offset: $('.active .page-link').text(),
                limit: $(".page-size").text()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })
    }

    return {
        init: init,
        goBack: goBack,
        reLoad: reLoad,
        editElecPrinter: editElecPrinter,
        elecPrinterStation: elecPrinterStation,
        showModel: showModel,
        exportElecPrinter: exportElecPrinter
    };
})();
